package io.gitee.macxiang.utils;

/** mime判断 */
public class Mime extends SQL {
  /** 根据文件名获取文件后缀 */
  public static String mime(String str) {
    String ret = "application/octet-stream";
    if (Datas.isEmpty(str) || Datas.strLen(str) > 12) {
      str = "octet-stream";
    }
    str = str.substring(str.lastIndexOf('.') + 1).toLowerCase();
    final String[] html = { "html", "htm", "shtml" },
        text = { "css", "xml", "mml", "txt", "jad", "wml", "htc" },
        img = { "xbm", "tif", "jfif", "ico", "tiff", "gif", "svg", "jpeg", "svgz", "jpg", "webp",
            "png", "bmp", "pjp", "apng", "pjpeg", "avif", "wbmp", "jng" },
        audio = { "mid", "midi", "kar", "mp3", "ogg", "m4a", "ra", "aac" },
        video = { "3gpp", "3gp", "mp4", "mpeg", "mpg", "mov", "webm", "flv", "m4v", "mng", "asf",
            "wmv", "avi" },
        tjs = { "js", "mjs" };
    if (Datas.arrFindIndex(html, str) >= 0) {
      ret = "text/html;charset=utf-8";
    } else if (Datas.arrFindIndex(text, str) >= 0) {
      ret = "text/" + str + ";charset=utf-8";
    } else if (Datas.arrFindIndex(img, str) >= 0) {
      ret = "image/" + str;
    } else if (Datas.arrFindIndex(audio, str) >= 0) {
      ret = "audio/" + str;
    } else if (Datas.arrFindIndex(video, str) >= 0) {
      ret = "video/" + str;
    } else if (Datas.arrFindIndex(tjs, str) >= 0) {
      ret = "application/javascript; charset=UTF-8";
    } else if (str.equals("json")) {
      ret = "application/json; charset=UTF-8";
    } else {
      switch (str) {
        case "woff":
          ret = "font/woff";
          break;
        case "woff2":
          ret = "font/woff2";
          break;
        case "ts":
          ret = "video/mp2t";
          break;
        case "m3u8":
          ret = "application/vnd.apple.mpegurl";
          break;

        default:
          ret = "application/" + str;
          break;
      }
    }

    return ret;
  }

}
